package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.web.entity.table.ReadingStudentTableDef;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.dingreading.cloud.web.entity.ReadingStudent;
import com.dingreading.cloud.web.mapper.ReadingStudentMapper;
import com.dingreading.cloud.web.service.ReadingStudentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-11-14
 */
@Service
public class ReadingStudentServiceImpl extends BaseServiceImpl<ReadingStudentMapper, ReadingStudent, ReadingStudentTableDef> implements ReadingStudentService {

    @Override
    protected ReadingStudentTableDef getTable() {
        return ReadingStudentTableDef.readingStudent;
    }

    @Override
    public ReadingStudent haveFullNameAndMob(String corpUid, String uid, String fullName, String mob) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.fullName.eq(fullName))
                .and(table.mob.eq(mob));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<ReadingStudent> getStudentByOpenid(String openid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.openid.eq(openid));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public ReadingStudent getStudent(String studentUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(studentUid));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public long getStudentSignCnt() {
        QueryCondition condition = QueryCondition.createEmpty();
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public boolean updateStudentImg(String studentUid, String img) {
        return UpdateChain.of(ReadingStudent.class)
                .set(table.img, img)
                .where(table.uid.eq(studentUid))
                .update();
    }

    @Override
    public long haveChannelUid(String channelUid) {
        return getMapper().selectCountByCondition(table.channelUid.eq(channelUid));
    }

    @Override
    public Map<String, Long> haveSignStudentBookCnt(List<String> bookNames) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.bookName.in(bookNames));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.count(table.id).as("cnt"), table.bookName)
                .from(table)
                .where(condition)
                .groupBy(table.bookName);

        List<Row> rows = getMapper().selectRowsByQuery(wrapper);

        Map<String, Long> map = new HashMap<>();
        for (String bookName : bookNames) {
            map.put(bookName, 0L);
        }
        if (CollectionUtils.isNotEmpty(rows)) {
            for (Row row : rows) {
                String bookName = row.getString("book_name");
                if (StringUtils.isNotBlank(bookName)) {
                    Long cnt = row.getLong("cnt");
                    map.put(bookName, NepUtil.nullToZero(cnt));
                }
            }
        }
        return map;
    }

}
